﻿using Project.Net.Domain.UserLogin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Net.Infra.UserLogin
{
    /// <summary>
    /// 密码加密
    /// </summary>
    public class PasswordHasher : IPasswordHasher
    {
        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public string Hash(string password)
        {
            return BCrypt.Net.BCrypt.HashPassword(password);
        }

        public bool IsTookWeak(string password)
        {
            // 可添加自定义的密码强度规则
            return password.Length < 8
                || !password.Any(char.IsDigit)
                || !password.Any(char.IsLetter);
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public bool Verify(string password, string hash)
        {
            return BCrypt.Net.BCrypt.Verify(password, hash);
        }
    }
}
